﻿using CarAppClient.Consts;
using CarAppClient.Controls.MessageUsercontrols;
using CarAppClient.Extension;
using CarAppClient.Helpers;
using CarAppClient.ViewModels;
using CarAppClient.Views.CarType.Views;
using CarAppDto.CarType.CountryDtos;
using CarAppDto.CarType.CarDtos;
using D.UtilCore.Dtos;
using System.Collections.ObjectModel;
using System.Xml.Linq;
using Autofac.Features.OwnedInstances;
using Newtonsoft.Json;
using CarAppClient.Views.Rule.Views;
using CarAppDto.Rule.RuleConfigDtos;
using CarAppClient.Views.CarVin.Views;
using CarAppDto.CarType.AreaDtos;
using CarAppDto.Rule.RuleMappingDtos;
using System.Data;
using CarAppDto.CarVin.VINDtos;
using D.WPF.MVVM.EventUtil;
using CarAppClient.Views.CarType.Views.Brand;
using CarAppClient.Views.CarType.Views.VehicleSeries;
using CarAppClient.Views.CarType.Views.CarType;
using CarAppClient.Views.CarType.Views.CarTypeVersion;
using CarAppClient.Views.CarParts;
using CarAppDto.Common;

namespace CarAppClient.Views.Rule.ViewModels.RuleConfig
{
    public class VINQueryViewModel : BaseCrudViewModel<RepRuleConfig>
    {
        #region 加载页面数据     
        RepRuleConfig _repRuleConfig;

       
        public VINQueryViewModel()
        {     
        }

        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="repCarType"></param>
        protected void LoadData()
        {
            Data = new ObservableCollection<RepRuleConfig>();
            //Query();
        }

        #region 查询条件

        private string? vin;
        /// <summary>
        /// 汽车码
        /// </summary>
        public string? VIN
        {
            get { return vin; }
            set { vin = value; RaisePropertyChanged(); }
        }

        /// <summary>
        /// VIN详情
        /// </summary>
        private ObservableCollection<VINMapping> dataRuleMapping;
        public ObservableCollection<VINMapping> DataRuleMapping
        {
            get { return dataRuleMapping; }
            set { dataRuleMapping = value; RaisePropertyChanged(); }
        }
        #endregion
        #region 查询


        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="repCarType"></param>
        protected override async void Query(object? obj = null)
        {
            if (Data != null)
            {
                Data.Clear();
            }
            else
            {
                Data = new ObservableCollection<RepRuleConfig>();
            }

            // 请求后端接口 
            //var res = await HttpHelper<List<RepRuleConfig>>.PostAsync(ApiUrl.GetRuleConfigByVIN, VIN);
            var res = await HttpHelper<List<RepRuleConfig>>.GetAsync($"{ApiUrl.GetRuleConfigByVIN}/{VIN}");

            //  判断结果
            if (res.Code == StatusCodeEnum.Waring)
            {
                MsgUtil.Warning(res.ErrorMsg);
            }
            else if (res.Code == StatusCodeEnum.Error)
            {
                MsgUtil.Error(res.ErrorMsg);
            }
            else if (res.Code == StatusCodeEnum.ServerError)
            {
                MsgUtil.Error("服务器异常");
            }
            else if (res.Code == StatusCodeEnum.OK)
            {
                // 绑定数据源到UI 
                if (res.Data != null)
                {
                    foreach (var item in res.Data)
                    {
                        item.IsSelected = false;
                        Data.Add(item);
                    }
                }
            }
            else
            {
                MsgUtil.Error(res.Code.ToString());
            }
        }
        #endregion
        #endregion


        protected override async void SelectedModelChange(object? obj = null) {
            if (obj == null) return;
          var ruleModel=(RepRuleConfig)obj;
            // 请求后端接口 
            // 请求后端接口 
            var res = await HttpHelper<List<RepRuleMapping>>.PostAsync(ApiUrl.GetRuleMappingByVIN, new RepRuleConfigAndVIN()
            {
                RuleId = ruleModel.Id,
                VIN = VIN
            }) ;
            DataRuleMapping = new ObservableCollection<VINMapping>();
            //  判断结果
            if (res.Code == StatusCodeEnum.Waring)
            {
                MsgUtil.Warning(res.ErrorMsg);
            }
            else if (res.Code == StatusCodeEnum.Error)
            {
                MsgUtil.Error(res.ErrorMsg);
            }
            else if (res.Code == StatusCodeEnum.ServerError)
            {
                MsgUtil.Error("服务器异常");
            }
            else if (res.Code == StatusCodeEnum.OK)
            {
                // 绑定数据源到UI 
                if (res.Data != null)
                {
                    foreach (var item in res.Data)
                    {
                        VINMapping vinMapping = new VINMapping();
                        vinMapping.Key=item.Key;
                        vinMapping.Val=item.Val;
                        vinMapping.RegStr = item.RegStr;
                        vinMapping.Descption=item.Descption;
                        vinMapping.Type = (int)item.Details.Type;
                        List<string> displayKeyList=new List<string>();
                        List<string> displayValueList = new List<string>();
                        if (item.Details.ValueDetailsList != null)
                        {
                            foreach (var itemValueDetails in item.Details.ValueDetailsList)
                            {
                                displayKeyList.Add(itemValueDetails.Key);
                                displayValueList.Add(itemValueDetails.Value);
                            }
                            vinMapping.DisplayKey = string.Join("\n", displayKeyList);
                            vinMapping.DisplayValue = string.Join("\n", displayValueList);
                        }
                        else
                        {
                            vinMapping.DisplayKey= item.Details?.ReferenceDetails?.Key;
                            vinMapping.DisplayValue = item.Details?.ReferenceDetails?.Value;
                            vinMapping.Id = item.Details?.ReferenceDetails?.Id;
                        }
                        
                        DataRuleMapping.Add(vinMapping);
                    }
                }
            }
            else
            {
                MsgUtil.Error(res.Code.ToString());
            }
        }


        #region 查询详情
        private VINMapping selectedVINMappingModel;
        /// <summary>
        /// 名称
        /// </summary>
        public VINMapping SelectedVINMappingModel
        {
            get { return selectedVINMappingModel; }
            set { selectedVINMappingModel = value; RaisePropertyChanged(); }
        }

        /// <summary>
        /// 详情查询事件
        /// </summary>
        private DelegateCommandT<VINMapping>? _queryVINMappingCommand;
        public DelegateCommandT<VINMapping> QueryVINMappingCommand => _queryVINMappingCommand ?? (_queryVINMappingCommand = new DelegateCommandT<VINMapping>(QueryVINMapping));
        protected  void QueryVINMapping(VINMapping? obj = null) {

            switch (obj.Type)
            {
                case 1: ShowBrand(obj.Id);break;
                case 2: ShowVehicleSeries(obj.Id); break;
                case 3: ShowCarType(obj.Id); break;
                case 4: ShowCarTypeVersion(obj.Id); break;
                case 5: ShowCarParts(obj.Id); break;
            }
        }

        /// <summary>
        /// 显示品牌详情
        /// </summary>
        /// <param name="id"></param>
        private void ShowBrand(string id)
        {
            Page_Brand_Modify view = new Page_Brand_Modify(id,ViewStatus.View);
            view.ShowDialog();
        }
        /// <summary>
        /// 显示车系
        /// </summary>
        /// <param name="id"></param>
        private void ShowVehicleSeries(string id)
        {
            Page_VehicleSeries_Modify view = new Page_VehicleSeries_Modify(id, viewStatus: ViewStatus.View);
            view.ShowDialog();
        }

        /// <summary>
        /// 车型
        /// </summary>
        /// <param name="id"></param>
        private void ShowCarType(string id)
        {
            Page_CarType_Modify view = new Page_CarType_Modify(id,viewStatus:ViewStatus.View);
            view.ShowDialog();
        }

        /// <summary>
        /// 车型版本
        /// </summary>
        /// <param name="id"></param>
        private void ShowCarTypeVersion(string id)
        {
            Page_CarTypeVersion_Modify view = new Page_CarTypeVersion_Modify(id, viewStatus: ViewStatus.View);
            view.ShowDialog();
        }

        private void ShowCarParts(string id)
        {
            Page_CarParts_Modify view = new Page_CarParts_Modify(id, viewStatus: ViewStatus.View);
            view.ShowDialog();
        }
        #endregion
    }

}
